Rust

RUST Documentation 따라 공부하기 12

2019.12.30


Managing Growing Projects with Packages, Crates, and Modules

  • Packages: A Cargo feature that lets you build, test, and share crates
  • Crates: A tree of modules that produces a library or executable
  • Modules and use: Let you control the organization, scope, and privacy of paths
  • Paths: A way of naming an item, such as a struct, function, or module

Defining Modules to Contorl Scope and Privacy

cargo new --lib restaurant # to create a new library
// src/lib.rs
mod front_of_house {
  pub mod hosting {
    pub fn add_to_waitlist() {}
    
    fn seat_at_table() {}
  }
  mod serving {
    fn take_order() {}
    fn serve_order() {}
        fn take_payment() {}
  }
}

// public API
pub fn eat_at_restaurant() {
  // Absolute path
  crate::front_of_house::hosting::add_to_waitlist();
  
  // Relative path
  front_of_house::hosting::add_to_waitlist();
}